home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 4
/
Apprentice-Release4.iso
/
Languages
/
Caml Light 0.7
/
Caml Light 0.7 source
/
src
/
appli
/
ui.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-07-04
|
5KB
|
224 lines
#include <Dialogs.h>
#include <Events.h>
#include <Files.h>
#include <LowMem.h>
#include <Memory.h>
#include <Resources.h>
#include <Stdlib.h>
#include <Types.h>
#include "gui.h"
#include "ui.h"
int ui_read (int fd, char * p, unsigned long n)
{
if (fd == 0){
return DoRead (p, n);
}else{
return read (fd, p, n);
}
}
int ui_write (int fd, char * p, unsigned long n)
{
if (fd == 1 || fd == 2) {
DoWrite (CAMLPrinter, p, n);
return n;
}else{
return write (fd, p, n);
}
}
void ui_gc_message (char *fmt, unsigned long arg)
{
char buf [100]; /* arrgh !! */
sprintf (buf, fmt, arg);
ui_write (2, buf, strlen (buf));
}
void ui_exit (int status)
{
if (status != 0){
NoteAlert(140, 0);
stop_working ();
while (1) LookEvent (15);
}else{
Quit ();
}
}
void ui_fatal_error (char *fmt, char *arg)
{
char buf [200]; /* arrgh !!! */
sprintf (buf, fmt, arg);
ui_write (2, buf, strlen (buf));
ui_exit (3);
}
static int maxcount = 30;
static int count = 30;
static unsigned long lastdate = 0;
void ui_periodic_action (void)
{
--count;
if (count <= 0){
unsigned long date;
LookEvent (0);
date = TickCount ();
if (date - lastdate < 15 && maxcount < 50) ++maxcount;
if (date - lastdate > 15 && maxcount > 10) --maxcount;
count = maxcount;
}
}
char *get_wd_name (short wd_refnum)
{
char buf [64];
char *temp, *result, *postfix = ":";
DirInfo cinfo;
WDPBRec wd;
wd.ioCompletion = NULL;
wd.ioNamePtr = NULL;
wd.ioVRefNum = wd_refnum;
wd.ioWDIndex = 0;
wd.ioWDProcID = 0;
wd.ioWDVRefNum = 0;
if (PBGetWDInfo (&wd, 0) != noErr) return postfix;
cinfo.ioDrParID = wd.ioWDDirID;
result = malloc (strlen (postfix) + 1);
if (result == NULL) Error (1);
strcpy (result, postfix);
do{
cinfo.ioCompletion = NULL;
cinfo.ioNamePtr = (StringPtr) buf;
cinfo.ioVRefNum = wd.ioWDVRefNum;
cinfo.ioFDirIndex = -1;
cinfo.ioDrDirID = cinfo.ioDrParID;
if (PBGetCatInfo ((CInfoPBPtr) &cinfo, 0) != noErr) return postfix;
p2cstr (buf);
temp = malloc (strlen (result) + strlen (buf) + 2);
if (temp == NULL){
free (result);
return postfix;
}
sprintf (temp, ":%s%s", buf, result);
free (result);
result = temp;
}while (cinfo.ioDrDirID != 2);
return result + 1;
}
char *ui_getenv (char *name)
{
Handle h;
h = GetNamedResource ('ENV ', (ConstStr255Param) name);
if (h == nil) return NULL;
HLock (h);
return *h;
}
int main (int argc, char *argv [])
{
#pragma unused (argc, argv)
int i;
int new_argc;
char *new_argv [6];
WDPBRec wd;
char *cur_dir = ":";
StringPtr curapname;
Handle h;
Initialize ();
wd.ioCompletion = NULL;
wd.ioNamePtr = NULL;
if (PBHGetVol (&wd, 0) == noErr){
cur_dir = get_wd_name (wd.ioVRefNum);
}
curapname = LMGetCurApName ();
new_argv [0] = malloc (curapname [0] + 1);
if (new_argv [0] == NULL) Error (1);
for (i = 0; i < curapname [0]; i++){
new_argv [0] [i] = curapname [i+1];
}
new_argv [0] [i] = '\0';
new_argv [1] = "-stdlib";
new_argv [2] = malloc (strlen (cur_dir) + 5);
if (new_argv [2] == NULL) Error (1);
sprintf (new_argv [2], "%slib:", cur_dir);
h = GetNamedResource ('ENV ', (ConstStr255Param) "\pLANG");
if (h == nil){
new_argc = 3;
}else{
HLock (h);
new_argv [3] = "-lang";
new_argv [4] = *h;
new_argc = 5;
}
new_argv [new_argc] = NULL;
return caml_main (new_argc, new_argv);
}
#ifdef __MWERKS__
/* Stubs for the console package. The calls for stdin, stdout, and stderr
are already intercepted by ui_write, ui_read, etc, thus the stubs will
never be called.
These stubs are adapted from "console.stubs.c" from CodeWarrior 6.
*/
#ifndef __CONSOLE__
#include <console.h>
#endif
short InstallConsole (short fd)
{
#pragma unused (fd)
#ifdef DEBUG
DebugStr ("\pUnexpected call to InstallConsole");
#endif
return 0;
}
void RemoveConsole (void)
{
#ifdef DEBUG
DebugStr ("\pUnexpected call to RemoveConsole");
#endif
}
long WriteCharsToConsole (char *buffer, long n)
{
#pragma unused (buffer, n)
#ifdef DEBUG
DebugStr ("\pUnexpected call to WriteCharsToConsole");
#endif
return 0;
}
long ReadCharsFromConsole (char *buffer, long n)
{
#pragma unused (buffer, n)
#ifdef DEBUG
DebugStr ("\pUnexpected call to ReadCharsFromConsole");
#endif
return 0;
}
char *__ttyname (long fildes)
{
#pragma unused (fildes)
#ifdef DEBUG
DebugStr ("\pUnexpected call to __ttyname");
#endif
return NULL;
}
#endif /* __MWERKS__ */